<?php

/**
 * @author ChessMax
 * @copyright 2010
 */
	 
    class Serv
    {
        public $mysqli;
        
        public $flashVars;
        
        public $answerObj;
        public $errorObj;
        
        public $prefix;
        
        /**
         * Была ли ошибка при создании?
         * */
        public $hasError = false;
        
                 
        function __construct($flashVars)
        { 
            $this->flashVars = $flashVars;
            
            if (Auther::authorized($flashVars) == false)  
            {
                $this->failed('Ошибка авторизации'); 
                return;
            }
            
            $this->prefix = TABLE_PREFIX;
            
            $this->mysqli = new mysqli(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS);
  
            if (!$this->mysqli) 
            {
                $this->failed('Ошибка подключения к базе.', $this->mysqli->error);  
                return;
            }
            
            $this->mysqli->query('set names utf8');
            
            $this->mysqli->select_db(DB_NAME); 
        }
        
        /**
         * Если нужно вернуть несколько объектов то ...
         * */
        public function addAnswerParam($key, $value)
        {
            $this->answer[$key] = $value;
        }
        
        public function complete()
        {
            $this->answer['success'] = true;
            return $this->answer; 
        }
        
        public function failed($error, $params = null)
        {
            if ($params != null)
            {
                $this->errorObj[] = $params;
            }
            
            $this->errorObj['success'] = false;
            $this->errorObj['error']   = $error;
            
            $this->hasError = true;
              
            return $this->errorObj;
        }
    } 
?>